<?php

class BedrijfFormStep2 extends Form
{
	const L_FIELD_REQUIRED = 'Dit veld is verplicht';

	public function __construct( $data = array() )
	{
		parent::__construct( );
		
		if( isset( $data['id'] ) )
		{
			$this->addChild( new InputHidden( 'id', $data['id'] ) );
		}
		
		$algemeen = new FieldSet( 'Contact gegevens' );
		
		$phone_office  = new InputText( 'phone_office', Bedrijf::getField('phone_office'), isset( $data[ 'phone_office' ] ) ? $data[ 'phone_office' ] : '', 'phone' );
		$phone_office->getValidator()->required( self::L_FIELD_REQUIRED );
		$algemeen->addChild( $phone_office );
		
		$phone_alternate = new InputText( 'phone_alternate', Bedrijf::getField('phone_alternate'), isset( $data[ 'phone_alternate' ] ) ? $data[ 'phone_alternate' ] : '', 'phone' );
		$phone_alternate->getValidator()->required( self::L_FIELD_REQUIRED );
		$algemeen->addChild( $phone_alternate );
		
		$email = new InputText( 'email', Bedrijf::getField('email'), isset( $data[ 'email' ] ) ? $data[ 'email' ] : '', 'email' );
		$email->getValidator()->required( self::L_FIELD_REQUIRED );
		$email->getValidator()->compareMatch( ElementValidation::REGEX_EMAIL, 'Wel een geldig e-mail adres invullen he!' );
		$algemeen->addChild( $email );
		
		$website = new InputText( 'website', Bedrijf::getField('website'), isset( $data[ 'website' ] ) ? $data[ 'website' ] : '', 'street' );
		$website->getValidator()->required( self::L_FIELD_REQUIRED );
		$algemeen->addChild( $website );
		
		$this->addChild( $algemeen );
		
		$company = new FieldSet( 'Bedrijfsgegevens' );
		
		$employees = new InputText( 'employees', Bedrijf::getField('employees'), isset( $data[ 'employees' ] ) ? $data[ 'employees' ] : '', 'postal' );
		$employees->getValidator()->required( self::L_FIELD_REQUIRED );
		$employees->getValidator()->compareMatch( ElementValidation::REGEX_NUMBER, 'U moet een geldig nummer invullen.' );
		$employees->getValidator()->method( array( $this, 'validateEmployees' ), 'Er moeten minstens 5 mensen bij dit bedrijf werken.' );
		$company->addChild( $employees );
		
		$description = new InputText( 'description', Bedrijf::getField('description'), isset( $data[ 'description' ] ) ? $data[ 'description' ] : '', 'street' );
		//$description->getValidator()->required( self::L_FIELD_REQUIRED );
		$company->addChild( $description );
		
		$manager = new InputText( 'manager', Bedrijf::getField('manager'), isset( $data[ 'manager' ] ) ? $data[ 'manager' ] : '', 'city' );
		$manager->getValidator()->required( self::L_FIELD_REQUIRED );
		$company->addChild( $manager );
				
		$this->addChild( $company );
		
		$this->addChild( new SubmitButton( 'previous', 'Stap terug' ) );
		$this->addChild( new SubmitButton( 'submit', 'Opslaan' ) );
	}
	
	public function validateEmployees( FormElement $fe )
	{
		return ( (int)$fe->getProperty('value') >= 5 );
	}
}
